package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.resources.RedisMessageConstant;
import com.itheima.resources.SMSUtils;
import com.itheima.resources.ValidateCodeUtils;
import com.itheima.service.OrderService;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Reference;
import org.omg.SendingContext.RunTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RequestMapping(value = "/order",name = "预约模块")
@RestController
public class OrderController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping(value = "/findSetMealById.do",name = "根据套餐id查询单个套餐信息")
    public Result findSetMealById(@RequestParam("id") String setMealId ){
        try {
            //查询套餐数据
            Setmeal setmeal = setMealService.findSetMealCommonById(setMealId);
            return new Result(true,"查询套餐成功",setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询套餐成功");
        }
    }

    @RequestMapping(value = "/SendVerifyCode.do",name = "发送验证码短信")
    public Result SendVerifyCode(@RequestParam("telephone") String telephone ){
        try {
            //产生4位数的验证码
            String code = ValidateCodeUtils.generateValidateCode(4);

            //将手机号作为key,验证码作为value存储到redis中,方便后台验证
            Jedis jedis = jedisPool.getResource();
            //存储到redis中,并设置存储时间 90秒.超过时间即失效
            jedis.setex(telephone+"_"+ RedisMessageConstant.SENDTYPE_ORDER,90,code);
            //释放jedis
            jedis.close();

            //发送短信
            SMSUtils.sendCode(telephone,code);

            return new Result(true,"短信验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "短信验证码发送失败");
        }
    }

    @RequestMapping(value = "/addOrder.do",name = "添加预约")
    public Result addOrder(@RequestBody Map map){
        try {
            Jedis jedis = jedisPool.getResource();
            //查询redis中存储的验证码
            String redisCode = jedis.get(map.get("telephone") + "_" + RedisMessageConstant.SENDTYPE_ORDER);
            //释放jedis
            jedis.close();

            //获取用户提交的验证码
            String code = (String) map.get("validateCode");

            if (redisCode==null){
                return new Result(false,"验证码已过时,请重新获取");
            }

            //比较验证码是否正确
            if (!redisCode.equals(code)){
                return new Result(false,"验证码错误");
            }
            //调用生产者添加预约订单
            Order order=orderService.addOrder(map);

            return new Result(true,"预约成功",order);
        } catch (RuntimeException e) {
            e.printStackTrace();
            //将报错信息返回回去
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约失败");
        }
    }

    @RequestMapping(value = "/findOrderSuccess.do",name = "预约成功回显预约消息")
    public Result findOrderSuccess(@RequestParam("id") String id ){
        try {
            //查询数据
            Map map=orderService.orderSuccess(id);
            return new Result(true,"查询套餐成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询套餐成功");
        }
    }
}
